﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_admin_save_property_attributes : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string hash = WebhelperMethods.URLHash(Request);
            if (string.IsNullOrWhiteSpace(hash))
            {
                Response.Redirect("login.aspx");
                return;
            }

            PomplingCustomers customer = PomplingCustomers.CustomerSelect(hash);
            if (string.IsNullOrWhiteSpace(customer.PomplingCustomerID))
            {
                Response.Redirect("login.aspx");
                return;
            }

            string sAddressId = WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request);
            if (string.IsNullOrWhiteSpace(sAddressId))
            {
                Response.Redirect("admin_save_address.aspx?c=" + Request["c"] + "&a=");
                return;
            }

            // create a blank entery for the list
            List<Lookup> propertyAttributesLookup = new List<Lookup>();
            Lookup lookupItem = new Lookup();
            lookupItem.LookupID = -1;
            lookupItem.LookupText = "";
            propertyAttributesLookup.Add(lookupItem);
            List<Lookup> listTemp = Lookup.GetLookupGroup("PropertyAttribute");

            // add the database records to the blank entery records.
            propertyAttributesLookup.AddRange(listTemp);

            LstPropertyAttribute.DataTextField = "LookupText";
            LstPropertyAttribute.DataValueField = "LookupID";
            LstPropertyAttribute.DataSource = propertyAttributesLookup;
            LstPropertyAttribute.DataBind();
            LstPropertyAttribute.Value = "-1";


            //List<PropertyAttribute> properties = PropertyAttribute.SelectpropertyAttributes(Int32.Parse( sAddressId));

            //if (properties.Count > 3)
            //{

            //    int rowHeightPx = 15;

            //    int borderStartHeightPx = 600;
            //    int borderStyleHeightPx = properties.Count * rowHeightPx;
            //    borderStyleHeightPx = borderStartHeightPx + borderStyleHeightPx;
            //    theForm.Style.Add("height", borderStyleHeightPx.ToString() + "px");
            //}
        }
    }

    protected void BtnAddAttribute_Click(object sender, EventArgs e)
    {
        string strLookupID = LstPropertyAttribute.Value;
        string lookupText = LstPropertyAttribute.Items[LstPropertyAttribute.SelectedIndex].Text;
        int lookupID = 0;
        Int32.TryParse(strLookupID, out lookupID);
        string attributeDescription = TxtAttributeDescription.Value;
        Lookup lookup = new Lookup();
        lookup.LookupText = lookupText;
        lookup.LookupID = lookupID;

        LstPropertyAttribute.SelectedIndex = 0;
        TxtAttributeDescription.InnerText = string.Empty;
        
        PropertyAttribute.AddAttribute(lookup, Int32.Parse(WebhelperMethods.GetUrlParamValue("a", Request)), attributeDescription);

        //int addressID = GetAddressID();
        string newUri = "redirect.aspx" + Request.Url.Query;
        
        Response.Redirect(newUri + "&pagefrom=admin_save_property_attributes");   
    }
    protected void BtnRemoveAttribute_Click(object sender, EventArgs e)
    {
        PropertyAttribute.Delete(Int32.Parse(WebhelperMethods.GetUrlParamValue("a", Request)));

        BtnRemoveAttribute.Enabled = false;
        string newUri = "redirect.aspx" + Request.Url.Query + "&pagefrom=admin_save_property_attributes";
        Response.Redirect(newUri);
        
    }
    protected void BtnNext_Click(object sender, EventArgs e)
    {
        string newUri = "admin_property_image_main.aspx" + Request.Url.Query;
        
        Response.Redirect(newUri);
    }
    protected void BtnPrevious_Click(object sender, EventArgs e)
    {
        string newUri = "admin_save_property_let_info.aspx" + Request.Url.Query;
        //Response.Redirect("save_property_let_info.aspx?WebPropertyID=" + GetWebPropertyID()+"&addressid="+GetAddressID());
        Response.Redirect(newUri);
    }
}